# coding:utf-8
import logging
import random
import time,os

logger = logging.getLogger()
logger.setLevel(logging.INFO)
rq = time.strftime('%Y-%m-%d', time.localtime(time.time()))
log_path = os.path.dirname(os.getcwd()) + '/Goods/Logs/'
log_name = log_path + rq + '.txt'  # 文件名字决定文件数量
logfile = log_name
fh = logging.FileHandler(logfile, mode='a+', encoding='utf-8')
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
logger.addHandler(fh)



def logtofile(message, fun):
    """
    写入日志
    :param message: 消息
    :return:
    """
    lt = time.strftime('%Y-%m-%d:%H-%M-%S', time.localtime(time.time()))
    fun("{lt}-{msg}".format(lt=lt, msg=message))


def GBK2312():
    """
    随机生成中文字符串
    :return:
    """
    head = random.randint(0xb0, 0xf7)
    body = random.randint(0xa1, 0xf9)  # 在head区号为55的那一块最后5个汉字是乱码,为了方便缩减下范围
    val = f'{head:x}{body:x}'
    str = bytes.fromhex(val).decode('gb2312')
    return str


